@@ -5,6 +5,8 @@ module Agents |
||
| 5 | 5 |
cannot_be_scheduled! |
| 6 | 6 |
cannot_create_events! |
| 7 | 7 |
|
| 8 |
+ before_validation :create_device, on: :create |
|
| 9 |
+ |
|
| 8 | 10 |
API_BASE = 'https://api.pushbullet.com/v2/' |
| 9 | 11 |
TYPE_TO_ATTRIBUTES = {
|
| 10 | 12 |
'note' => [:title, :body], |
@@ -54,7 +56,7 @@ module Agents |
||
| 54 | 56 |
|
| 55 | 57 |
def validate_options |
| 56 | 58 |
errors.add(:base, "you need to specify a pushbullet api_key") if options['api_key'].blank? |
| 57 |
- create_device if options['device_id'].blank? |
|
| 59 |
+ errors.add(:base, "you need to specify a device_id") if options['device_id'].blank? |
|
| 58 | 60 |
errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type']) |
| 59 | 61 |
TYPE_TO_ATTRIBUTES[options['type']].each do |attr| |
| 60 | 62 |
errors.add(:base, "you need to specify '#{attr.to_s}' for the type '#{options['type']}'") if options[attr].blank?
|
@@ -105,6 +107,7 @@ module Agents |
||
| 105 | 107 |
end |
| 106 | 108 |
|
| 107 | 109 |
def create_device |
| 110 |
+ return if options['device_id'].present? |
|
| 108 | 111 |
safely do |
| 109 | 112 |
response = request(:post, 'devices', basic_auth.merge(body: {nickname: 'Huginn', type: 'stream'}))
|
| 110 | 113 |
self.options[:device_id] = response['iden'] |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 |
class AddTypeOptionAttributeToPushbulletAgents < ActiveRecord::Migration |
| 2 | 2 |
def up |
| 3 |
- Agents::PushbulletAgent.all.each do |agent| |
|
| 3 |
+ Agents::PushbulletAgent.find_each do |agent| |
|
| 4 | 4 |
if agent.options['type'].nil? |
| 5 | 5 |
agent.options['type'] = 'note' |
| 6 | 6 |
agent.save! |
@@ -9,7 +9,7 @@ class AddTypeOptionAttributeToPushbulletAgents < ActiveRecord::Migration |
||
| 9 | 9 |
end |
| 10 | 10 |
|
| 11 | 11 |
def down |
| 12 |
- Agents::PushbulletAgent.all.each do |agent| |
|
| 12 |
+ Agents::PushbulletAgent.find_each do |agent| |
|
| 13 | 13 |
if agent.options['type'].present? |
| 14 | 14 |
agent.options.delete 'type' |
| 15 | 15 |
agent.save(validate: false) |
@@ -35,8 +35,7 @@ describe Agents::PushbulletAgent do |
||
| 35 | 35 |
|
| 36 | 36 |
it "should try do create a device_id" do |
| 37 | 37 |
@checker.options['device_id'] = nil |
| 38 |
- mock(@checker).create_device |
|
| 39 |
- expect(@checker).to be_valid |
|
| 38 |
+ expect(@checker).not_to be_valid |
|
| 40 | 39 |
end |
| 41 | 40 |
|
| 42 | 41 |
it "should require fields based on the type" do |
@@ -142,6 +141,7 @@ describe Agents::PushbulletAgent do |
||
| 142 | 141 |
stub_request(:post, "https://token:@api.pushbullet.com/v2/devices"). |
| 143 | 142 |
with(:body => "nickname=Huginn&type=stream"). |
| 144 | 143 |
to_return(:status => 200, :body => '{"iden": "udm0Tdjz5A7bL4NM"}', :headers => {})
|
| 144 |
+ @checker.options['device_id'] = nil |
|
| 145 | 145 |
@checker.send(:create_device) |
| 146 | 146 |
expect(@checker.options[:device_id]).to eq('udm0Tdjz5A7bL4NM')
|
| 147 | 147 |
end |